package com.ckm.aop;

import com.ckm.annotation.MongoDS;
import com.ckm.context.DynamicMongoContextHolder;
import com.ckm.datasource.DynamicMongoFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class DynamicSwitchAop {

    @Autowired
    private DynamicMongoFactory dynamicMongoFactory;

    @Before("@annotation(com.ckm.annotation.MongoDS)")
    public void doBefore(JoinPoint joinPoint){
        MongoDS annotation = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(MongoDS.class);
        String value = annotation.value();
        DynamicMongoContextHolder.setMongoContext(value);
    }

    @After("@annotation(com.ckm.annotation.MongoDS)")
    public void doAfter(){
        DynamicMongoContextHolder.pollMongoContext();
    }
}
